package org.exam.blog.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.exam.blog.domain.Course;
import org.exam.blog.query.MessageQuery;
import org.exam.blog.service.ICourseService;
import org.exam.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@Api(tags = "用户提示")
public class CourseController {
    @ApiOperation(value = "查询单个接口")
    @PutMapping
    public JsonResult add(@RequestBody Course course) {
        try {
            if (course.getId() == null) {
                iCourseService.add(course);
                return JsonResult.success();
            } else {
                iCourseService.update(course);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();

        }
    }

    @Autowired
    private ICourseService iCourseService;

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        try {
            iCourseService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/{id}")
    public Course get(@PathVariable("id") Long id) {
        return iCourseService.get(id);
    }

    @GetMapping("/echarts")
    public List<Course> getAll() {
        return iCourseService.getAll();
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            iCourseService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageInfo<Course> queryPage(@RequestBody MessageQuery messageQuery) {
        return iCourseService.queryPage(messageQuery);
    }




}
